package middlewares

import (
	"time"

	"github.com/gin-gonic/gin"
	"github.com/mars/pkg/loggers"
)

func Logger() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		path := ctx.Request.URL.RequestURI()
		clientIP := ctx.ClientIP()
		method := ctx.Request.Method

		start := time.Now()
		ctx.Next()
		end := time.Now()
		latency := end.Sub(start).Milliseconds()

		statusCode := ctx.Writer.Status()

		loggers.Infofc(ctx, "status: [%d], path: %s, clientIP: %s, requestMethod: %s, latency: %dms",
			statusCode, path, clientIP, method, latency)
	}
}
